package com.boge.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.R;
import com.boge.modules.shop.entity.GoodsEntity;
import com.boge.modules.shop.entity.GoodsnewEntity;
import com.boge.modules.shop.entity.GoodstypeEntity;
import com.boge.modules.shop.entity.GoodstypenewEntity;
import com.boge.modules.shop.service.*;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysRoleEntity;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.entity.SysUserRoleEntity;
import com.boge.modules.sys.service.SysRoleService;
import com.boge.modules.sys.service.SysUserRoleService;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AppController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysUserRoleService userRoleService;

    @Autowired
    private GoodstypeService goodstypeService;

    @Autowired
    private GoodstypenewService goodstypenewService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsnewService goodsnewService;


    @RequestMapping("/auth/home/info")
    //@RequiresPermissions("house:tarea:list")
    public R list(@RequestParam Map<String, Object> params){
        // 查询 商品类别
        List<GoodstypeEntity> types = goodstypeService.list();
        // 查询 甄选类别'
        List<GoodstypenewEntity> typeZxs = goodstypenewService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("types",types);
        map.put("typeZxs",typeZxs);
        return R.ok().put("code",200).put("message","success")
                .put("data",map)
                ;
    }

    @RequestMapping("/auth/home/goodsByTypeId")
    //@RequiresPermissions("house:tarea:list")
    public R goodsByTypeId(@RequestParam Map<String, Object> params){
        String typeId = (String) params.get("typeId");
        List<GoodsEntity> goods = goodsService
                .list(new LambdaQueryWrapper<GoodsEntity>()
                        .eq(GoodsEntity::getTypeId,Integer.parseInt(typeId)));
        if(goods != null&& goods.size() > 0){
            for (GoodsEntity good : goods) {
                good.setImage(WebController.BASE_URL+good.getImage());
            }
        }
        return R.ok().put("code",200).put("message","success")
                .put("data",goods)
                ;
    }

    @RequestMapping("/auth/home/goodsList")
    //@RequiresPermissions("house:tarea:list")
    public R goodsList(@RequestParam Map<String, Object> params){
        List<GoodsEntity> goods = goodsService
                .list(new LambdaQueryWrapper<GoodsEntity>()
                        );
        if(goods != null&& goods.size() > 0){
            for (GoodsEntity good : goods) {
                good.setImage(WebController.BASE_URL+good.getImage());
            }
        }
        return R.ok().put("code",200).put("message","success")
                .put("data",goods)
                ;
    }

    @RequestMapping("/auth/home/goodsZxByTypeId")
    //@RequiresPermissions("house:tarea:list")
    public R goodsZxByTypeId(@RequestParam Map<String, Object> params){
        String typeId = (String) params.get("typeId");
        List<GoodsnewEntity> goods = goodsnewService
                .list(new LambdaQueryWrapper<GoodsnewEntity>()
                        .eq(GoodsnewEntity::getTypeId,Integer.parseInt(typeId)));
        if(goods != null&& goods.size() > 0){
            for (GoodsnewEntity good : goods) {
                good.setImg(WebController.BASE_URL+good.getImg());
            }
        }
        return R.ok().put("code",200).put("message","success")
                .put("data",goods)
                ;
    }



    @PostMapping("/auth/registerApp")
    @ResponseBody
    public R registerApp(@RequestBody SysUserEntity user){
        user.setStatus(1);
        //user.setPassword("123456");
        if(StringUtils.isBlank(user.getNickname())){
            user.setNickname(user.getUsername());
        }

        // 绑定角色
        userService.saveUser(user);
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name","用户");
        List<SysRoleEntity> list = roleService.list(wrapper);
        if(list != null && list.size() > 0){
            SysRoleEntity sysRoleEntity = list.get(0);
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setUserId(user.getUserId());
            entity.setRoleId(sysRoleEntity.getRoleId());
            userRoleService.save(entity);
        }
        return R.ok();
    }
}
